home *** CD-ROM | disk | FTP | other *** search
- /*
- * sethostname - set current name of machine
- */
- #include "lib.h"
- #include <sys/file.h>
- #include <sys/param.h>
- #include <errno.h>
-
- #ifndef MAXHOSTNAMELEN
- # define MAXHOSTNAMELEN 64 /* just like BSD */
- #endif
-
- #define min( a, b ) (( a < b ) ? a : b )
-
- extern int errno;
- /* static char namebuf[ MAXHOSTNAMELEN ]; */
-
- int sethostname( name, namelen )
- char *name;
- int namelen;
- {
- int fd;
-
- if ( getuid() != 0 ) { /* must be superuser to do this */
- errno = EPERM;
- return( -1 );
- }
- if (creat( HOSTNAME, 0644 ) < 0) /* (re)create /etc/localhostname */
- return( -1 );
- if ((fd = open( HOSTNAME, O_WRONLY )) < 0) {
- errno = EIO;
- return( -1 );
- }
-
- name[ min( namelen, MAXHOSTNAMELEN ) ] = '\n';
- if (write( fd, name, min( namelen, MAXHOSTNAMELEN )) < 0) {
- errno = EIO;
- return( -1 );
- }
- close( fd );
- return( 0 );
- }
-